#include <QObject>
#include <qtcpserver.h>
#include <qtcpsocket.h>

#include "_Interface/_global_datastruct.h"
#include "Package/simpletcpserverdll_global.h"
#include "Package/simpletcpsocketdll_global.h"
#include "Backend/wyplccom.h"

WyPLCCom::WyPLCCom(){
    mWyFswData.ForceData = 0;
    mWyFswData.SpindlSpeedStatus = false;
    mWyFswData.SpindlSpeedDir = false;
    mWyFswData.SpindlSpeedData = 0;
    StateMachine = StateMachine_ForceSensor;
}

WyPLCCom::~WyPLCCom(){}

short WyPLCCom::PlcConnect(QString ip, int port){
    COM_addTcpClient(ip,port,this->toWyPLCSocket);
    COM_connectToServer(this->toWyPLCSocket);
    return 1;
}

short WyPLCCom::ReadForceSensorData(double &Data){
    Data = mWyFswData.ForceData;
    return 1;
}
short WyPLCCom::SetSpindleStatusSetting(bool status){
    mWyFswData.SpindlSpeedStatus = status;
    if(mWyFswData.SpindlSpeedStatus == false) StateMachine = StateMachine_SpindleStatus_OFF;
    else                                      StateMachine = StateMachine_SpindleStatus_ON;
    return 1;
}
short WyPLCCom::SetSpindleSpeedSetting(int Data){
    mWyFswData.SpindlSpeedData = Data;
    this->mWyFswData.SpindleH = mWyFswData.SpindlSpeedData >> 8;
    this->mWyFswData.SpindleL = mWyFswData.SpindlSpeedData;
    StateMachine = StateMachine_SpindleSpeed;
    return 1;
}
short WyPLCCom::SetSpindleSpeedDir(bool dir){
    mWyFswData.SpindlSpeedDir = dir;
    if(mWyFswData.SpindlSpeedStatus == false) StateMachine = StateMachine_SpindleDir_RIGHT;
    else                                      StateMachine = StateMachine_SpindleDir_Left;
    return 1;
}

short WyPLCCom::PLCRecvDataDeal(){
    switch (this->StateMachine) {
    case StateMachine_ForceSensor:
        mWyFswData.ForceDataH = RecvRawWyPLCData[1];
        mWyFswData.ForceDataL = RecvRawWyPLCData[2];
        mWyFswData.ForceData =(mWyFswData.ForceDataH << 8) | mWyFswData.ForceDataL;
        break;
    case StateMachine_SpindleStatus_ON:
        this->StateMachine = StateMachine_ForceSensor;
        break;
    case StateMachine_SpindleStatus_OFF:
        this->StateMachine = StateMachine_ForceSensor;
        break;
    case StateMachine_SpindleSpeed:
        this->StateMachine = StateMachine_ForceSensor;
        break;
    case StateMachine_SpindleDir_RIGHT:
        this->StateMachine = StateMachine_ForceSensor;
        break;
    case StateMachine_SpindleDir_Left:
        this->StateMachine = StateMachine_ForceSensor;
        break;
    default:
        break;
    }
    return 1;
}
short WyPLCCom::toPLCLoopRun(){

    QByteArray SenData;
    char Ask_ForceAsk[3] ={0x01, 0x01, 0x04};
    char Com_SpindleOn[4] = {0x02, 0x01, 0x01, 0x01}; //开主轴
    char Com_SpindleOff[4] = {0x2, 0x01, 0x01, 0x00}; //关主轴
    char Com_DirRight[4] ={0x02, 0x01, 0x02, 0x01}; //方向1
    char Com_DirLeft[4] ={0x02, 0x01, 0x02, 0x00}; //方向2
    char Com_RPM[5] ={0x02, 0x01, 0x03, 0x00, 0x00}; //转速

    if(this->AskDelay < 25){
        this->AskDelay = this->AskDelay + 1;
    }
    else{
        if(!COM_getClientSendStatus(this->toWyPLCSocket)){
            switch (StateMachine) {
            case StateMachine_ForceSensor:
                SenData = Ask_ForceAsk;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            case StateMachine_SpindleStatus_ON:
                SenData = Com_SpindleOn;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            case StateMachine_SpindleStatus_OFF:
                SenData = Com_SpindleOff;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            case StateMachine_SpindleSpeed:
                Com_RPM [3] = this->mWyFswData.spindleH;
                Com_RPM [4] = this->mWyFswData.spindleL;
                SenData = Com_RPM;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            case StateMachine_SpindleDir_RIGHT:
                SenData = Com_DirRight;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            case StateMachine_SpindleDir_Left:
                SenData = Com_DirLeft;
                COM_clientSendData(this->toWyPLCSocket,SenData);
                break;
            default:
                break;
            }
        }
        this->AskDelay = 0;
    }
    if(COM_getClientReadStatus(this->toWyPLCSocket)){
        COM_clientReadData(this->toWyPLCSocket,RecvRawWyPLCData);
        this->PLCRecvDataDeal();
    }
    return 1;
}
